Class ConsoleFieldScanner<T>

  • Type Parameters:
    T - The type of the value being read

    public class ConsoleFieldScanner<T>
    extends java.lang.Object
    Generic console function for getting a new value for an entity field from the user.

    Example:

     Integer num = new ConsoleFieldScanner(Integer::parseInt, (Integer i) -> i > 0, null)
         .scan("Give me a number")
     
    Produces this output:
     Give me a number: <user inputs 123 and presses Enter>
     
    Leading to the variable num having the value 123.
    Since:
    ExerciseSheet04
    Version:
    1
    Author:
    Jonas Altrock (ew20b126@technikum-wien.at)
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static java.util.Scanner defaultScanner
      The default input scanner.
      static boolean NOT_SKIPPABLE
      constant to allow readable indication of a non-skippable input
      java.util.Scanner scanner
      The scanner object in use.
      boolean skippable
      Whether the input can be skipped (by pressing Enter for example).
      static boolean SKIPPABLE
      constant to allow readable indication of a skippable input
      java.util.function.Function<java.lang.String,​T> transformer
      The input to value transformer function.
      java.util.function.Predicate<T> validator
      The input value validator function.
    • Constructor Summary

      Constructors 
      Constructor Description
      ConsoleFieldScanner​(java.util.function.Function<java.lang.String,​T> transformer, java.util.function.Predicate<T> validator, java.util.Scanner scanner)
      Create a scanner for a single value.
      ConsoleFieldScanner​(java.util.function.Function<java.lang.String,​T> transformer, java.util.function.Predicate<T> validator, java.util.Scanner scanner, boolean skippable)
      Create a scanner for a single value.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      protected java.util.Scanner getDefaultScanner()
      Initialise the default scanner object.
      T scan​(java.lang.String message)
      Prompt the user for an input value.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • SKIPPABLE

        public static final boolean SKIPPABLE
        constant to allow readable indication of a skippable input
        See Also:
        Constant Field Values
      • NOT_SKIPPABLE

        public static final boolean NOT_SKIPPABLE
        constant to allow readable indication of a non-skippable input
        See Also:
        Constant Field Values
      • defaultScanner

        public static java.util.Scanner defaultScanner
        The default input scanner. Uses System.in if not set from outside.
      • skippable

        public boolean skippable
        Whether the input can be skipped (by pressing Enter for example).
      • transformer

        public java.util.function.Function<java.lang.String,​T> transformer
        The input to value transformer function.
      • validator

        public java.util.function.Predicate<T> validator
        The input value validator function.
      • scanner

        public java.util.Scanner scanner
        The scanner object in use.
    • Constructor Detail

      • ConsoleFieldScanner

        public ConsoleFieldScanner​(java.util.function.Function<java.lang.String,​T> transformer,
                                   java.util.function.Predicate<T> validator,
                                   java.util.Scanner scanner)
        Create a scanner for a single value.
        Parameters:
        transformer - a function that transforms the input string to a value
        validator - a function that validates the given value
        scanner - optional Scanner to use, pass null to use the default scanner (System.in)
      • ConsoleFieldScanner

        public ConsoleFieldScanner​(java.util.function.Function<java.lang.String,​T> transformer,
                                   java.util.function.Predicate<T> validator,
                                   java.util.Scanner scanner,
                                   boolean skippable)
        Create a scanner for a single value.
        Parameters:
        transformer - a function that transforms the input string to a value
        validator - a function that validates the given value
        scanner - optional Scanner to use, pass null to use the default scanner (System.in)
        skippable - whether the user is allowed to skip entry by just pressing Enter
    • Method Detail

      • getDefaultScanner

        protected java.util.Scanner getDefaultScanner()
        Initialise the default scanner object.
        Returns:
        the default scanner
      • scan

        public T scan​(java.lang.String message)
        Prompt the user for an input value.
        Parameters:
        message - string to print before prompt input
        Returns:
        a value or null, if no value was given